/**
 * 作者：陈文斌 on 2019/1/10.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/model/main_scoped_model.dart';
import 'package:crm/model/ClientManager/contact_info_model.dart';
import 'package:crm/view/Tile/list_tile_normal.dart';
import 'package:crm/view/Tile/tile_space_between.dart';
import 'package:crm/page/ClientManager/add_contact_page.dart';
import 'package:crm/page/ClientManager/contact_cart_edit_page.dart';

class AddClientContactListSection extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _AddClientContactListSectionState();
}

class _AddClientContactListSectionState
    extends State<AddClientContactListSection> {
  textStyle(double fontSize) {
    return TextStyle(
      color: AppColors.DetailTextColor,
      fontSize: fontSize ?? AppFontSizes.Font15,
      fontFamily: AppFontFamilies.PingFangSC,
      fontWeight: FontWeight.w500,
    );
  }

  List<ContactInfoModel> _contactsList;

  @override
  Widget build(BuildContext context) {
    MainModel contactManagerSM = MainModel().of(context);
    _contactsList = contactManagerSM.contactsInfoList;

    /// 区头区域
    Widget _headerSection = TileSpaceBetween(
      title: Text(
        '联系人',
        style: textStyle(AppFontSizes.Font13),
      ),
      height: 34,
    );

    /// 联系人列表项区域
    Widget ContactItemSection(ContactInfoModel contactInfo) {
      return ListTileNormal(
        title: Row(
          children: <Widget>[
            Expanded(
              child: Text(
                '${contactInfo.name}-${contactInfo.position}',
                style: textStyle(AppFontSizes.Font12),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Expanded(
              child: Text(
                '${contactInfo.phone}',
                style: textStyle(AppFontSizes.Font12),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            )
          ],
        ),
        trailing: Container(
          width: 18,
          height: 18,
          decoration: BoxDecoration(
            color: AppColors.MainColor,
            borderRadius: BorderRadius.circular(9),
          ),
        ),
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => ContactCartEditPage(
                    contactInfo: contactInfo,
                  ),
            ),
          );
        },
        arrowVisible: true,
      );
    }

    /// 添加按钮区域
    Widget _addButtonSection = ListTileNormal(
      leading: Icon(
        Icons.add,
        color: AppColors.DetailTextColor,
        size: 20,
      ),
      leadingRightPadding: 8,
      title: Text(
        '添加…',
        style: textStyle(AppFontSizes.Font15),
      ),
      dividerVisible: true,
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => AddContactPage(),
          ),
        );
      },
    );

    return Container(
      height: (_contactsList.length + 1) * 45.0 + 34,
      child: ListView.builder(
        physics: const NeverScrollableScrollPhysics(),
        itemCount: _contactsList.length + 2,
        itemBuilder: (context, i) {
          if (i == 0) {
            /// 第一行返回区头
            return _headerSection;
          }
          if (i < _contactsList.length + 1) {
            /// 联系人列表项
            ContactInfoModel _contactInfo = _contactsList[i - 1];
            return ContactItemSection(_contactInfo);
          } else {
            /// 添加按钮
            return _addButtonSection;
          }
        },
      ),
    );
  }
}
